<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <!-- 选项 -->
      <el-checkbox v-for="item in allRoles" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      allRoles: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    //   获取角色列表
    async getRoleList() {
      const { rows } = await getRoleList()
      this.allRoles = rows
    },
    // 获取用户的当前角色
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      //   console.log('这是父组件触发')
      this.checkList = roleIds
    },
    // 确认按钮
    async btnOk() {
      // try {
      // 发请求
      const roleIds = this.checkList
      const id = this.userId
      console.log(id)
      await assignRoles({ id, roleIds })
      //   提示用户
      this.$message.success('修改成功')
      // 关闭弹窗
      //   this.$emit('update:showRoleDialog', false)
      this.$parent.showRoleDialog = false
      // } catch (error) {
      //   console.log(error)
      // }
    },
    // 取消按钮
    btnCancel() {
      // 清空列表
      this.checkList = []
      // 关闭弹窗
      //   this.$emit('update:showRoleDialog', false)
      this.$parent.showRoleDialog = false
    }
  }
}
</script>

<style>
</style>
